# -*- coding:utf-8 -*-

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.views.generic.simple import direct_to_template
from django.core.urlresolvers import reverse
from tsuchinoco.tasks.models import Tasks
from tsuchinoco.tasks.models import TasksForm
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect

def taskindex(request):
    """
    タスクの一覧を表示する
    """
    if request.method == 'GET':
        if request.session.get('user_id',None) == None:
            return HttpResponseRedirect('/')
        tasks = Tasks.objects.all()
        print Tasks.objects.all().count()
        return render_to_response('taskindex.html',
                                  context_instance=RequestContext(request,
                                                                  {'user_id':request.session.get('user_id',
                                                                                                 None),
                                                                   'task_list':tasks}))
    elif request.method == 'POST':
        pass

def newedit(request,user_id):
    """
    """
    if request.method == 'GET':
        form = TasksForm()
        print form
        return render_to_response('taskedit.html',
                                  context_instance=RequestContext(request,
                                                                  {'form':form,
                                                                   'user_id':user_id,
                                                                   'error_message':''}))
    elif request.method == 'POST':
        form = TasksForm(request.POST)
        if form.is_valid():
            task = form.save()
            task.user = user_id
            task.save()
            return HttpResponseRedirect('/task/' +user_id+ '/edit/')        
        else:
            print form.errors
            return render_to_response('taskedit.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'user_id':user_id,
                                                                       'error_message':form.errors}))
def taskedit(request,user_id,task_id):
    """
    """
    if request.method == 'GET':
        print user_id
        print request.session.get('user_id',None)
        if int(user_id) == request.session.get('user_id',0):
        #print  "task_id:%d"%task_id
            task = Tasks.objects.get(pk=int(task_id))
            print task.detail
            form = TasksForm(None,instance=task)
            return render_to_response('taskview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'task_id':task_id,
                                                                       'user_id':user_id,
                                                                       'error_message':''}))
        else:
           pass
    elif request.method == 'POST':
        task = Tasks.objects.get(pk=int(task_id))
        form = TasksForm(request.POST, instance=task)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/task/')
        else:
            print form.errors
            return render_to_response('taskview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'task_id':task_id,
                                                                       'user_id':user_id,
                                                                       'error_message':form.errors}))
 
